import store from '@/store'

function authPermission(permission) {
  const all_permission = '*:*:*'
  const permissions = store.getters && store.getters.permissions
  if (!permission || !permission.length) return false
  return permissions.some(v => all_permission === v || v === permission)
}

function authRole(role) {
  const super_admin = 'admin'
  const roles = store.getters && store.getters.roles
  if (!role || !role.length) return false
  return roles.some(v => super_admin === v || v === role)
}

export default {
  // 验证用户是否具备某权限
  hasPermi: authPermission,
  // 验证用户是否含有指定权限，只需包含其中一个
  hasPermiOr: permissions => permissions.some(i => authPermission(i)),
  // 验证用户是否含有指定权限，必须全部拥有
  hasPermiAnd: permissions => permissions.every(i => authPermission(i)),
  // 验证用户是否具备某角色
  hasRole: authRole,
  // 验证用户是否含有指定角色，只需包含其中一个
  hasRoleOr: roles => roles.some(i => authRole(i)),
  // 验证用户是否含有指定角色，必须全部拥有
  hasRoleAnd: roles => roles.every(i => authRole(i))
}
